package com.example.lss.news;

import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.example.lss.news.adapter.VideoAdapter;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.utils.CommonUtil;
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper;

public class VideoActivity extends AppCompatActivity {
    private String[] url = new String[]{"http://wscdn.miaopai.com/xkx4/2015/1209/8c1b5274bde25fcc5f7371ea23c5fc90.mp4",
            "http://wscdn.miaopai.com/xkx4/2016/0921/705586033fd196abe0d21d8159e8de38.mp4",
            "http://wscdn.miaopai.com/xkx4/2015/1023/d4fb209c21a333b576e60a81e974e754.mp4",
            "http://gslb.miaopai.com/stream/N9A9EwF97LxsMBYph3~97ddLdqc9TARHJCSTMw__.mp4?vend=miaopai&ssig=d32e08ee27136e8937be07eb723d330f&time_stamp=1535988691850","" +
            "http://gslb.miaopai.com/stream/aciJTFQNgdLuMJ7pRSJx30gvgt7luMXpXKPJMA__.mp4?vend=miaopai&ssig=a62ed5d50622baa6ced75f088f8c2321&time_stamp=1535990294516","" +
            "http://gslb.miaopai.com/stream/D3sRA25VdDPM~HTfcV5CQ8uiJyIOP-fW4c0XMg__.mp4?vend=miaopai&ssig=8d7d0eb764db71d9b6fd65a1eefd98aa&time_stamp=1535990294585","" +
            "http://gslb.miaopai.com/stream/IiicrmM7CGW~15fX0B0DG0tR4KHOC8J6QMfLsg__.mp4?vend=miaopai&ssig=c26439229c3cc0fbafb7ab353cb4c3b5&time_stamp=1535990294568","" +
            "http://gslb.miaopai.com/stream/clJuK8dkJ0fMx-AK1jljhs7oarPIQvZE1XySjw__.mp4?vend=miaopai&ssig=8b8859959a9e1b7e9bb2473dc45870c1&time_stamp=1535990294541","" +
            "http://gslb.miaopai.com/stream/KM1oPGG4By~yaq7Y~QBtX4sqoRRW5OuMOk7tAQ__.mp4?vend=miaopai&ssig=4d5ff6dbd40f6223ffef805b407d9118&time_stamp=1535990294605"};

    ListView videoList;

    RelativeLayout activityListVideo;

    GSYVideoHelper smallVideoHelper;

    VideoAdapter listVideoAdapter;

    GSYVideoHelper.GSYVideoHelperBuilder gsySmallVideoHelperBuilder;

    int lastVisibleItem;

    int firstVisibleItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        videoList = findViewById(R.id.video_list);
        activityListVideo = findViewById(R.id.activity_list_video);

        //创建小窗口帮助类
        smallVideoHelper = new GSYVideoHelper(this);

        //配置
        gsySmallVideoHelperBuilder = new GSYVideoHelper.GSYVideoHelperBuilder();
        gsySmallVideoHelperBuilder
                .setHideStatusBar(true)
                .setNeedLockFull(true)
                .setCacheWithPlay(true)
                .setShowFullAnimation(false)
                .setRotateViewAuto(false)
                .setLockLand(true)
                .setVideoAllCallBack(new GSYSampleCallBack() {
                    @Override
                    public void onQuitSmallWidget(String url, Object... objects) {
                        super.onQuitSmallWidget(url, objects);
                        //大于0说明有播放,//对应的播放列表TAG
                        if (smallVideoHelper.getPlayPosition() >= 0 && smallVideoHelper.getPlayTAG().equals(VideoAdapter.TAG)) {
                            //当前播放的位置
                            int position = smallVideoHelper.getPlayPosition();
                            //不可视的是时候
                            if ((position < firstVisibleItem || position > lastVisibleItem)) {
                                //释放掉视频
                                smallVideoHelper.releaseVideoPlayer();
                                listVideoAdapter.notifyDataSetChanged();
                            }
                        }

                    }
                });

        smallVideoHelper.setGsyVideoOptionBuilder(gsySmallVideoHelperBuilder);

        listVideoAdapter = new VideoAdapter(this, smallVideoHelper, gsySmallVideoHelperBuilder, url);

        listVideoAdapter.setRootView(activityListVideo);
        videoList.setAdapter(listVideoAdapter);


        videoList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                VideoActivity.this.firstVisibleItem = firstVisibleItem;
                lastVisibleItem = firstVisibleItem + visibleItemCount;
                //大于0说明有播放,//对应的播放列表TAG
                if (smallVideoHelper.getPlayPosition() >= 0 && smallVideoHelper.getPlayTAG().equals(VideoAdapter.TAG)) {
                    //当前播放的位置
                    int position = smallVideoHelper.getPlayPosition();
                    //不可视的是时候
                    if ((position < firstVisibleItem || position > lastVisibleItem)) {
                        //如果是小窗口就不需要处理
                        if (!smallVideoHelper.isSmall()) {
                            //小窗口
                            int size = CommonUtil.dip2px(VideoActivity.this, 150);
                            smallVideoHelper.showSmallVideo(new Point(size, size), false, true);
                        }
                    } else {
                        if (smallVideoHelper.isSmall()) {
                            smallVideoHelper.smallVideoToNormal();
                        }
                    }
                }
            }

        });
    }


    @Override
    public void onBackPressed() {
        if (smallVideoHelper.backFromFull()) {
            return;
        }
        super.onBackPressed();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        smallVideoHelper.releaseVideoPlayer();
        GSYVideoManager.releaseAllVideos();
    }
}
